package com.liu.network.socket;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;

public class SocketClient implements Runnable {
	Socket socket = null;

	String key = "";

	public SocketClient(Socket socket, String key) {
		this.socket = socket;
		this.key = key;
	}

	public void run() {
		OutputStream os = null;
		ByteArrayOutputStream out = null;
		try {
			while (true) {
				os = socket.getOutputStream();
				long stime = System.currentTimeMillis();
				if ("tcp_ucp_flow".equals(key)) {
					out = tcp_ucp_flow(1, 5);
				} else if ("pstat".equals(key)) {
					out = pstat(2, 5);
				} else if ("pflow".equals(key)) {
					out = pflow(2, 5);
				}
				System.err.println("一个" + key + "数据包开始发送！！！\r\n字节长度为：" + out.size());
				os.write(out.toByteArray());
				System.err.println("一个" + key + "数据包发送完毕！！！\r\n花费时间为：" + (System.currentTimeMillis() - stime) + "ms");
				os.flush();// 清空io中的数据
			}
		} catch (IOException e) {
			e.printStackTrace();
		}  finally {
			if (out != null) {
				try {
					out.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (os != null) {
				try {
					os.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

	public ByteArrayOutputStream tcp_ucp_flow(int submultiple, int pow) throws IOException {
		int max = (int) (submultiple * Math.pow(10, pow));
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		String key = null;
		String value = null;
		int leng;
		for (int i = 0; i < max; i++) {// udp:flow
			key = "udp:flow";
			value = "1560131603, 72, 0, 0, 0, 3232236156, NULL,1920103026,NULL, 64424, 53, 1, 0, 395, 0, 1, 0, 76, 0, 2, 0, 471, 0,0.500000, 0.500000, 0.838641, 0.161359, 0, 0, 21193,47.185394,47.185394, 18638.228516, 3586.089600, 1560131560010644,1560131560031837, 0, 4294967295, 8356419218836404200";
			leng = key.length() + value.length();
			out.write(intToBytes(leng));
			out.write(intToBytes(key.length()));
			out.write(intToBytes(value.length()));
			out.write(key.getBytes());
			out.write(value.getBytes());
		}

		for (int i = 0; i < max; i++) {// tcp:flow
			key = "tcp:flow";
			value = "1560131633, 72, 0, 0, 0, 3232236324, NULL,3072617490,NULL, 51729, 36688, 6, 759, 0, 0, 0, 5, 1, 0, 1, 1, 3, 1,453, 0,0, 0, 0, 0, 0, 0, 0, 1, 0, 1460, 2, 65536, 65516, 0, 399, 1,399,29227, 29158, 29290, 0.000000, 0.000000, 0.000000, 0.000000,0.000000, 0.000000, 0.600000, 0.744848, 4.610111, 583.179077,1013403942, 4, 260, 0, 0, 0, 4, 1, 0, 1, 1, 1, 1, 74, 0, 0,0, 0,0, 0, 0, 0, 1, 0, 1360, 7, 14720, 13600, 0, 20, 1, 20, 65190,223, 195020, 0.000000, 0.000000, 0.000000, 0.000000,0.000000,0.000000, 0.400000, 0.255152, 3.073408, 199.771500,212029613,223, 29290, 29513, 1301487, 1560131597002824,1560131598304311,0, 4294967295, 8677994484123678733";
			leng = key.length() + value.length();
			out.write(intToBytes(leng));
			out.write(intToBytes(key.length()));
			out.write(intToBytes(value.length()));
			out.write(key.getBytes());
			out.write(value.getBytes());
		}
		return out;
	}

	public ByteArrayOutputStream pstat(int submultiple, int pow) throws IOException {
		int max = (int) (submultiple * Math.pow(10, pow));
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		String key = null;
		String value = null;
		int leng;
		for (int i = 0; i < max; i++) {// p:stat
			key = "P:stat:link";
			value = "1560131573, 72, 255, 29753658, 17049880, 70746, 65429";
			leng = key.length() + value.length();
			out.write(intToBytes(leng));
			out.write(intToBytes(key.length()));
			out.write(intToBytes(value.length()));
			out.write(key.getBytes());
			out.write(value.getBytes());
		}

		for (int i = 0; i < max; i++) {// p:stat
			key = "P:stat:network";
			value = "1560131573, 71, 253, 255, 0, 0, 0, 0";
			leng = key.length() + value.length();
			out.write(intToBytes(leng));
			out.write(intToBytes(key.length()));
			out.write(intToBytes(value.length()));
			out.write(key.getBytes());
			out.write(value.getBytes());
		}

		for (int i = 0; i < max; i++) {// p:stat
			key = "P:stat:transport";
			value = "1560131573, 71, 2, 255, 0, 0, 0, 0";
			leng = key.length() + value.length();
			out.write(intToBytes(leng));
			out.write(intToBytes(key.length()));
			out.write(intToBytes(value.length()));
			out.write(key.getBytes());
			out.write(value.getBytes());
		}

		for (int i = 0; i < max; i++) {// p:stat
			key = "P:stat:application";
			value = "1560131573, 72, 1, 1, 119, 0, 0, 0, 0, 141, 309,478";
			leng = key.length() + value.length();
			out.write(intToBytes(leng));
			out.write(intToBytes(key.length()));
			out.write(intToBytes(value.length()));
			out.write(key.getBytes());
			out.write(value.getBytes());
		}
		return out;
	}

	public ByteArrayOutputStream pflow(int submultiple, int pow) throws IOException {
		int max = (int) (submultiple * Math.pow(10, pow));
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		String key = null;
		String value = null;
		int leng;
		for (int i = 0; i < max; i++) {// p:flow
			key = "P:flow:ipv4";
			value = "1560131453, 72, 3232236317, 2899908878, 57531, 443,6,1, 1, 66, 60, 20, 0, 11, 1, 1560131344843, 1560131442590,5912961334290265872, 0, 0, 962784134003, 176939654128322";
			leng = key.length() + value.length();
			out.write(intToBytes(leng));
			out.write(intToBytes(key.length()));
			out.write(intToBytes(value.length()));
			out.write(key.getBytes());
			out.write(value.getBytes());
		}

		for (int i = 0; i < max; i++) {// p:flow
			key = "P:flow:totalv4";
			value = "1560131453, 72, 3232237410, 1931315921, 44836, 80,6, 5,5, 561, 571, 0, 0, 10, 1, 1560131394158, 1560131394311,6492812980453750594, 0, 1, 90520738408197, 17693965412";
			leng = key.length() + value.length();
			out.write(intToBytes(leng));
			out.write(intToBytes(key.length()));
			out.write(intToBytes(value.length()));
			out.write(key.getBytes());
			out.write(value.getBytes());
		}

		for (int i = 0; i < max; i++) {// p:flow
			key = "P:flow:ipv6";
			value = "1560131453, 72, ff15::efc0:988f,fdf3:2f8a:9cfd:0:ecc7:dac5:3948:dc81, 6771, 6771, 17, 2, 0,402,0, 864, 462, 0, 1560131449234, 1560131451237,7203215139741218681, 0, 0, 56298158725263, 36232803451623";
			leng = key.length() + value.length();
			out.write(intToBytes(leng));
			out.write(intToBytes(key.length()));
			out.write(intToBytes(value.length()));
			out.write(key.getBytes());
			out.write(value.getBytes());
		}

		for (int i = 0; i < max; i++) {// p:flow
			key = "P:flow:totalv6";
			value = "1560131453, 72, fe80::2cfb:825e:5c5f:881e,ff02::1:3,51172, 5355, 17, 0, 3, 0, 252, 658784, 211, 1, 1560131401251,1560131401664, 6584933363163707209, 0, 1, 75361035190029,56294136414211";
			leng = key.length() + value.length();
			out.write(intToBytes(leng));
			out.write(intToBytes(key.length()));
			out.write(intToBytes(value.length()));
			out.write(key.getBytes());
			out.write(value.getBytes());
		}
		return out;
	}

	public static byte[] intToBytes(int value) {
		return new byte[] { (byte) ((value >> 24) & 0xFF), (byte) ((value >> 16) & 0xFF), (byte) ((value >> 8) & 0xFF),
				(byte) (value & 0xFF) };
	}

	public static void main(String[] args) {
		String[] key = { "tcp_ucp_flow", "pstat", "pflow" };
		SocketClient client = null;
		Socket socket = new Socket();
		SocketAddress address = new InetSocketAddress("192.168.3.11", 8888);
		try {
			socket.connect(address);
			for (int i = 0; i < 3; i++) {
				client = new SocketClient(socket, key[i]);
				new Thread(client).start();
				Thread.sleep(3000);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

	}
}
